package listener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import service.IBookService;
import service.Impl.IBookServiceImpl;
import vo.Book;

import javax.servlet.ServletContextEvent;
import java.util.ArrayList;
import java.util.List;

public class ServletContextListener implements javax.servlet.ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        List<Book> books=new ArrayList<>();
        IBookServiceImpl bookService=new IBookServiceImpl();
        books=bookService.getAllBook();
        List<Book>art=new ArrayList<>();
        List<Book>edu=new ArrayList<>();
        List<Book>child=new ArrayList<>();
        List<Book>social=new ArrayList<>();

        for(Book book:books){
            if(book.getType().equals("教育")){
                edu.add(book);
            }else if(book.getType().equals("文艺")){
                art.add(book);
            }else if(book.getType().equals("儿童")){
                child.add(book);
            }else  if(book.getType().equals("人文")){
                social.add(book);
            }
        }

        List<List> bookList = new ArrayList<>();
        bookList.add(art);
        bookList.add(edu);
        bookList.add(child);
        bookList.add(social);

        servletContextEvent.getServletContext().setAttribute("bookList",bookList);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
